<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="1500" destroy-on-close @closed="$emit('closed')" align-center>
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" label-width="130" ref="dialogForm">
            <el-row :gutter="1">
                <el-col :span="6">
                    <el-form-item label="编号" prop="other_code">
                        <el-input v-model="form.other_code" placeholder="编号(不填写自动生成)" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="名称" prop="other_name">
                        <el-input v-model="form.other_name" placeholder="请输入名称" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="日期" prop="other_date">
                        <el-date-picker v-model="form.other_date" type="date" placeholder="请选择日期" :value-format="'YYYY-MM-DD'" style="width:100%" />
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="仓库" prop="ware_id">
                        <el-select v-model="ware_id" filterable style="width: 100%" placeholder="仓库" @change="wareChange" clearable>
                            <el-option v-for="item in wareList" :key="item.id" :label="item.ware_name" :value="item.id" />
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-col :span="12">
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" placeholder="请输入备注" type="textarea" clearable></el-input>
                </el-form-item>
            </el-col>
            <el-form-item label="入库产品清单" prop="detail">
                <el-button type="primary" size="small" icon="el-icon-plus" @click="checkProduct" v-if="mode!='turn'">选择产品</el-button>
                <sc-form-table ref="table" v-model="form.detail" :addTemplate="addTemplate" placeholder="暂无数据" height="400" hideAdd show-summary>
                    <el-table-column label="产品型号" align="center">
                        <template #default="scope">
                            {{scope.row.goods_model}}
                        </template>
                    </el-table-column>
                    <el-table-column label="颜色" align="center">
                        <template #default="scope">
                            <el-select v-model="scope.row.color_type" filterable style="width: 100%" placeholder="颜色">
                                <el-option v-for="item in colorList" :key="item.id" :label="item.name" :value="item.id" />
                            </el-select>
                        </template>
                    </el-table-column>
                    <el-table-column label="方向" align="center">
                        <template #default="scope">
                            <el-select v-model="scope.row.direction_type" filterable style="width: 100%" placeholder="方向">
                                <el-option v-for="item in directionList" :key="item.id" :label="item.name" :value="item.id" />
                            </el-select>
                        </template>
                    </el-table-column>
                    <el-table-column label="速比" align="center" width="120">
                        <template #default="scope">
                            {{scope.row.speed_name}}
                        </template>
                    </el-table-column>
                    <el-table-column label="法兰" align="center" width="120">
                        <template #default="scope">
                            {{scope.row.flange_name}}
                        </template>
                    </el-table-column>
                    <el-table-column label="孔数" align="center" width="120">
                        <template #default="scope">
                            {{scope.row.hole}}
                        </template>
                    </el-table-column>
                    <!-- <el-table-column prop="color_name" label="颜色" align="center"></el-table-column> -->
                    <el-table-column label="数量" prop="number" align="center">
                        <template #default="scope">
                            <el-input v-model="scope.row.number" placeholder="数量" @input="totalCompute" @focus="onFocus($event)"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column label="仓库" align="center">
                        <template #default="scope">
                            <el-select v-model="scope.row.ware_id" filterable style="width: 100%" placeholder="仓库">
                                <el-option v-for="item in wareList" :key="item.id" :label="item.ware_name" :value="item.id" />
                            </el-select>
                        </template>
                    </el-table-column>
                    <el-table-column label="备注" align="center">
                        <template #default="scope">
                            <el-input v-model="scope.row.remark" placeholder="备注"></el-input>
                        </template>
                    </el-table-column>
                </sc-form-table>
            </el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
    <select-goods v-if="dialog.goods" ref="goodsDialog" @success="goodsUpdate" @closed="dialog.goods=false"></select-goods>
</template>

<script>
import selectGoods from '@/views/goods/info/selectGoods.vue';
	export default {
    components: { selectGoods },
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看',
				},
				visible: false,
				isSaveing: false,
				form: {
					id:"",
					other_code: "",
					other_name: "",
                    other_type: 1,
					remark: "",
					other_date: this.moment().format('YYYY-MM-DD'),
                    detail:[],
				},
                addTemplate:{},
                dialog: {
                    goods: false,
                },
                wareList:[],
                ware_id: "",
                colorList: [],
                directionList: [],
			}
		},
		mounted() {
            this.getWare()
            this.getColor() // 颜色
            this.getDirection() // 方向
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
                this.$refs.dialogForm.validate(async (valid) => {
                    if (valid) {
                        this.isSaveing = true;
                        var res  = [];
                        if (this.form.id) {
                            res = await this.$API.ware.other.edit.post(this.form);
                        }else{
                            res = await this.$API.ware.other.add.post(this.form);
                        }
                        this.isSaveing = false;
                        if(res!=''){
                            if(res.code == 1){
                                this.$emit('success', this.form, this.mode)
                                this.visible = false;
                                this.$message.success("操作成功")
                            }else{
                                this.$alert(res.message, "提示", {type: 'error'})
                            }
                        }
                    }
                })
			},
			//表单注入数据
			async setData(data){
				this.form.id = data.id
                var res = await this.$API.ware.other.detail.get({id:data.id});
                this.form.other_code = res.data.other_code
                this.form.other_name = res.data.other_name
                this.form.other_date = res.data.other_date
                this.form.remark = res.data.remark
                let array = []
                res.data.detail.map(item=>{
                    array.push({
                        id:item.id,
                        goods_id:item.goods_id,
                        goods_model:item.goods.goods_model,
                        speed_name:item.goods && item.goods.speed_ratio && item.goods.speed_ratio.name,
                        flange_name:item.goods && item.goods.flange && item.goods.flange.name,
                        // color_name:item.goods && item.goods.color && item.goods.color.name,
                        color_type: item.color_type,
                        direction_type: item.direction_type,
                        hole:item.goods.hole,
                        number:item.number,
                        ware_id:item.ware_id,
                        remark:item.remark,
                    })
                })
                this.form.detail = array
			},
            // 选择产品
            checkProduct(){
                this.dialog.goods = true
                this.$nextTick(() => {
                    this.$refs.goodsDialog.open('add')
                })
            },
            // 选择产品返回
            goodsUpdate(data){
                let array = []
                data.map((item)=>{
                    array.push({
                        goods_id:item.id,
                        goods_model:item.goods_model,
                        speed_name:item.speed_ratio && item.speed_ratio.name,
                        flange_name:item.flange && item.flange.name,
                        hole:item.hole,
                        ware_id:this.ware_id?this.ware_id:'',
                    })
                })
                // let existingIds = this.form.detail.map(item => item.goods_id);
                // for(let i = array.length - 1; i >= 0; i--){
                //     if(existingIds.includes(array[i].goods_id)){
                //         array.splice(i, 1);
                //     }
                // }
                this.form.detail = [...this.form.detail,...array]
            },
            onFocus(event){
                event.target.select();
            },
            async getColor(){
                var res = await this.$API.common.getDictData.get({ type: "color" });
                let array = []
                res.data.color.map(item=>{
                    array.push({
                        id:Number(item.value),
                        name:item.name
                    })
                })
                this.colorList = array;
            },
            async getDirection(){
                var res = await this.$API.common.getDictData.get({ type: "direction" });
                let array = []
                res.data.direction.map(item=>{
                    array.push({
                        id:Number(item.value),
                        name:item.name
                    })
                })
                this.directionList = array;
            },
            // 获取仓库
            async getWare(){
                var res = await this.$API.goods.ware.all.get();
                this.wareList = res.data;
                if(this.mode == 'add'){
                    res.data.map(item=>{
                        if(item.is_default == 1){
                            this.ware_id = item.id
                        }
                    })
                }
            },
            wareChange(e){
                this.form.detail.map(item => item.ware_id = e)
            },
		}
	}
</script>

<style>
</style>
